package com.example.oracleviewer.service;

import com.example.oracleviewer.entity.UserCard;
import com.example.oracleviewer.repository.UserCardRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户卡片服务类
 */
@Slf4j
@Service
public class UserCardService {
    
    @Autowired
    private UserCardRepository userCardRepository;
    
    /**
     * 查询所有用户卡片数据
     */
    public List<UserCard> getAllUserCards() {
        log.info("开始查询所有用户卡片数据");
        try {
            List<UserCard> userCards = userCardRepository.findAllUserCards();
            log.info("成功查询到 {} 条用户卡片数据", userCards.size());
            return userCards;
        } catch (Exception e) {
            log.error("查询用户卡片数据失败", e);
            throw new RuntimeException("查询用户卡片数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询
     */
    public UserCard getUserCardByUserId(String userId) {
        log.info("根据用户ID查询: {}", userId);
        try {
            UserCard userCard = userCardRepository.findByUserId(userId);
            log.info("查询到用户卡片: {}", userCard != null ? userCard.getUserId() : "null");
            return userCard;
        } catch (Exception e) {
            log.error("根据用户ID查询失败", e);
            throw new RuntimeException("根据用户ID查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据更新时间查询（大于指定时间）
     */
    public List<UserCard> getUserCardsByUpdateTime(String time) {
        log.info("根据更新时间查询: time={}", time);
        try {
            List<UserCard> userCards = userCardRepository.findByUpdateTimeGreaterThan(time);
            log.info("查询到 {} 条用户卡片数据", userCards.size());
            return userCards;
        } catch (Exception e) {
            log.error("根据更新时间查询失败", e);
            throw new RuntimeException("根据更新时间查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据更新时间查询（使用TO_TIMESTAMP）
     */
    public List<UserCard> getUserCardsByUpdateTimeTimestamp(String time) {
        log.info("根据更新时间查询(TO_TIMESTAMP): time={}", time);
        try {
            List<UserCard> userCards = userCardRepository.findByUpdateTimeGreaterThanTimestamp(time);
            log.info("查询到 {} 条用户卡片数据", userCards.size());
            return userCards;
        } catch (Exception e) {
            log.error("根据更新时间查询失败(TO_TIMESTAMP)", e);
            throw new RuntimeException("根据更新时间查询失败(TO_TIMESTAMP): " + e.getMessage());
        }
    }
    
    /**
     * 根据用户编号查询
     */
    public List<UserCard> getUserCardsByUserNumb(String userNumb) {
        log.info("根据用户编号查询: userNumb={}", userNumb);
        try {
            List<UserCard> userCards = userCardRepository.findByUserNumb(userNumb);
            log.info("查询到 {} 条用户卡片数据", userCards.size());
            return userCards;
        } catch (Exception e) {
            log.error("根据用户编号查询失败", e);
            throw new RuntimeException("根据用户编号查询失败: " + e.getMessage());
        }
    }
} 